package com.crm.system.service.impl.callCenter;

import com.beust.jcommander.internal.Lists;
import com.crm.common.model.Enum.StatisticalPeriodEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.callCenter.CallDurationVO;
import com.crm.model.vo.callCenter.CallStatisticsVO;
import com.crm.model.vo.callCenter.CallTimesStatisticsVO;
import com.crm.model.vo.callCenter.CallVO;
import com.crm.service.callCenter.CallFormDbService;
import com.crm.system.service.callCenter.CallResultRankSerice;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 业绩排名-呼叫中心
 * @Author lixq-f
 * @Date 2021/4/28
 * @Version V1.0
 **/
@Service
public class CallResultRankSericeImpl implements CallResultRankSerice {

    @Autowired
    private CallFormDbService callFormDbService;

    /**
     * @Description: 呼叫通话次数统计
     * @Param: [dateType, accountId]
     * @Return: com.crm.model.vo.callCenter.CallStatisticsVO
     * @Author: lixq-f
     * @Date: 2021/4/28
     **/
    @Override
    public CallStatisticsVO selectCallStatistics(Integer dateType, String accountId) {

        Date startTm = StatisticalPeriodEnum.getStartDateByPeriod(dateType);
        Date endTm = StatisticalPeriodEnum.getEndDateByPeriod(dateType);
        CallStatisticsVO callStatisticsVO = callFormDbService.
                selectCallStatistics(accountId, startTm, endTm);
        return callStatisticsVO;
    }

    /**
     * @Description: 呼叫次数折线图
     * @Param: [dateType, accountId]
     * @Return: com.crm.model.vo.callCenter.CallTimesStatisticsVO
     * @Author: lixq-f
     * @Date: 2021/4/28
     **/
    @Override
    public CallTimesStatisticsVO selectCallTimesStatistics(Integer dateType, String accountId) {
        Date startTm = StatisticalPeriodEnum.getStartDateByPeriod(dateType);
        Date endTm = StatisticalPeriodEnum.getEndDateByPeriod(dateType);
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        List<CallVO<BigDecimal>> avgList = null;
        List<CallVO<Long>> empLst = null;
        if (StatisticalPeriodEnum.THIS_YEAR.getCode() == dateType) {
            avgList = callFormDbService.selectCallAverageTimesByMonth(
                    userAccount.getCompanyId(), startTm, endTm);
            empLst = callFormDbService.selectEmpCallTimesByMonth(accountId, startTm, endTm);
        } else {
            avgList = callFormDbService.selectCallAverageTimesByDay(
                    userAccount.getCompanyId(), startTm, endTm);
            empLst = callFormDbService.selectEmpCallTimesByDay(accountId, startTm, endTm);
        }

        Map<String, BigDecimal> avgMap = null;
        if (!CollectionUtils.isEmpty(avgList)) {
            avgMap = avgList.stream()
                    .collect(Collectors.toMap(CallVO::getTime, CallVO::getVal));
        }

        Map<String, Long> empMap = null;
        if (!CollectionUtils.isEmpty(empLst)) {
            empMap = empLst.stream()
                    .collect(Collectors.toMap(CallVO::getTime, CallVO::getVal));
        }

        List<String> initTimes = StatisticalPeriodEnum.getInitTime(dateType);
        List<BigDecimal> averages = Lists.newArrayList();
        List<Long> empNums = Lists.newArrayList();
        for (String initTime : initTimes) {
            if (Objects.nonNull(avgMap)) {
                BigDecimal count = Optional.ofNullable(avgMap.get(initTime)).orElse(new BigDecimal(0.0));
                averages.add(count);
            } else {
                averages.add(new BigDecimal(0.0));
            }
            if (Objects.nonNull(empMap)) {
                Long count = Optional.ofNullable(empMap.get(initTime)).orElse(0L);
                empNums.add(count);
            } else {
                empNums.add(0L);
            }
        }
        CallTimesStatisticsVO result = new CallTimesStatisticsVO();
        result.setTimes(initTimes);
        result.setCallAverages(averages);
        result.setCurrentEmpCallNums(empNums);

        return result;
    }


    /**
     * 呼叫时长统计
     *
     * @param dateType
     * @param accountId
     * @return
     */
    @Override
    public CallDurationVO selectCallDurationStatistics(Integer dateType, String accountId) {

        Date startTm = StatisticalPeriodEnum.getStartDateByPeriod(dateType);
        Date endTm = StatisticalPeriodEnum.getEndDateByPeriod(dateType);
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();

        CallDurationVO durationVO = callFormDbService.
                selectCallDurationStatistics(accountId, userAccount.getCompanyId(), startTm, endTm);
        if (Objects.isNull(durationVO)){
            return new CallDurationVO();
        }

        Integer ge90Rank = null,ge60lt90Rank=null,ge30lt60Rank=null,lt30Rank=null;
        if (CommonUtils.isAdmin(userAccount.getUserId())){//超管
             ge90Rank = callFormDbService.
                    selectge90SecendsRank(accountId, null, startTm, endTm);
             ge60lt90Rank = callFormDbService
                    .selectge60lt90SecendsRank(accountId, null, startTm, endTm);
             ge30lt60Rank = callFormDbService
                    .selectge30lt60SecendsRank(accountId, null, startTm, endTm);
             lt30Rank = callFormDbService
                    .selectlt30SecendsRank(accountId, null, startTm, endTm);
        }else {//普管
            String companyId = userAccount.getCompanyId();
            ge90Rank = callFormDbService.
                    selectge90SecendsRank(accountId, companyId, startTm, endTm);
            ge60lt90Rank = callFormDbService
                    .selectge60lt90SecendsRank(accountId, companyId, startTm, endTm);
            ge30lt60Rank = callFormDbService
                    .selectge30lt60SecendsRank(accountId, companyId, startTm, endTm);
            lt30Rank = callFormDbService
                    .selectlt30SecendsRank(accountId, companyId, startTm, endTm);
        }
        durationVO.setGe90SecendsRank(Optional.ofNullable(ge90Rank).orElse(null));
        durationVO.setGe60lt90SecendsRank(Optional.ofNullable(ge60lt90Rank).orElse(null));
        durationVO.setGe30lt60SecendsRank(Optional.ofNullable(ge30lt60Rank).orElse(null));
        durationVO.setLt30SecendsRank(Optional.ofNullable(lt30Rank).orElse(null));

        return durationVO;
    }


}
